package com.wangfei.lecture.fanweiyunsuanfu;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

public class LambdaTest2 {
    @Test
    public void test() {
        happyTiem(500, new Consumer<Double>() {
            @Override
            public void accept(Double aDouble) {
                System.out.println("learning too much, get " + aDouble);
            }
        });
        System.out.println("*********************");
        happyTiem(400, money -> System.out.println("getting " + money));
    }

    public void happyTiem(double money, Consumer<Double> consumer) {
        consumer.accept(money);
    }

    @Test
    public void test2() {
        List<String> list = Arrays.asList("北京", "南京", "天津", "东京", "吴京", "普京", "牛津");
        List<String> filterString1 = filterString(list, new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.contains("京");
            }
        });
        System.out.println(filterString1);
        System.out.println("+++++++++++++++++++++++++");
        List<String> filterString2 = filterString(list, s -> s.contains("京"));
    }
    //根据给定的规则，过滤集合中的字符串。此规则由Predicate的方法决定
    public List <String> filterString(List<String> list, Predicate<String> pre){
        List<String> filterList = new ArrayList<>();

        for (String s : list) {
            if(pre.test(s)){
                filterList.add(s);
            }
        }

        return filterList;
    }

}
